using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemyMove : MonoBehaviour {


    public Texture[] texturesAnimsSkull;
    public Texture[] texturesAnimsMummie;
    public AudioClip clip;
    public Vector2 startPosition;
    public List<Vector2> path;
    public float speed;

    private Vector3 _savePos;
    private Const.ENNEMY_STATE _currentState;
    public Const.ACdirection _direction;
    public Const.ACdirection _saveDirection;

	// Use this for initialization
	void Start () 
    {
        _savePos = this.transform.position;
        AudioSource.PlayClipAtPoint(clip, this.transform.position);
        GetSpeed();

        Texture tex = null;
        switch (this.GetComponent<Enemy>().Type)
        {
            case Const.ENNEMY_TYPE.SQUELETTE:
                tex = texturesAnimsSkull[((int)_currentState)];
                break;

            case Const.ENNEMY_TYPE.MOMIE:
                tex = texturesAnimsMummie[((int)_currentState)];
                break;
        }

        
        this.GetComponent<Anim>().speed = 0.1f;
        this.GetComponent<Anim>().resetTimer(tex, 4);

        _saveDirection = Const.ACdirection.DOWN;//(Const.ACdirection)((int)_direction + 1);
	}
	
	// Update is called once per frame
	void Update () 
    {
        UpdateTexture();
	}

    void LateUpdate()
    {
        
    }

    void GetSpeed()
    {
        Const.ENNEMY_TYPE typeEnnemi = this.GetComponent<Enemy>().Type;

        switch (typeEnnemi)
        {
            case Const.ENNEMY_TYPE.SQUELETTE:
                speed = GDConst.SPEED_SKULL;
                break;
            case Const.ENNEMY_TYPE.MOMIE:
                speed = GDConst.SPEED_MUMMIE;
                break;
            case Const.ENNEMY_TYPE.CAT:
                speed = GDConst.SPEED_CAT;
                break;
            case Const.ENNEMY_TYPE.MONK:
                speed = GDConst.SPEED_MONK;
                break;
            case Const.ENNEMY_TYPE.ARTEFACT:
                speed = GDConst.SPEED_ARTEFACT;
                break;
        }
    }

    public virtual void Restart()
    {
        if (speed == 0)
            GetSpeed();
        
        this.transform.position = new Vector3(startPosition.x, Const.heightPerso, startPosition.y);

        AnimationClip clip = new AnimationClip();
        float currentTime = 0;
        int indexPath = 0;
        Vector2 oldPath = startPosition;
        
        AnimationCurve curveX = new AnimationCurve();
        AnimationCurve curveY = new AnimationCurve();
        AnimationCurve curveZ = new AnimationCurve();

        foreach (Vector2 v in path)
        {
            float diffX = v.x - oldPath.x;
            if (diffX == 0)
            {
                diffX = v.y - oldPath.y;
            }
            diffX = Mathf.Abs(diffX);

            curveX.AddKey(new Keyframe(currentTime, oldPath.x));
            curveX.AddKey(new Keyframe(currentTime + diffX * speed, v.x));

            curveY.AddKey(new Keyframe(currentTime, Const.heightPerso));
            curveY.AddKey(new Keyframe(currentTime + diffX * speed, Const.heightPerso));

            curveZ.AddKey(new Keyframe(currentTime, oldPath.y));
            curveZ.AddKey(new Keyframe(currentTime + diffX * speed, v.y));

            currentTime += diffX * speed;
            oldPath = v;
        }

        float diff = startPosition.x - oldPath.x;
        if (diff == 0)
        {
            diff = startPosition.y - oldPath.y;
        }
        diff = Mathf.Abs(diff);

        curveX.AddKey(new Keyframe(currentTime, oldPath.x));
        curveX.AddKey(new Keyframe(currentTime + diff * speed, startPosition.x));

        curveY.AddKey(new Keyframe(currentTime, Const.heightPerso));
        curveY.AddKey(new Keyframe(currentTime + diff * speed, Const.heightPerso));

        curveZ.AddKey(new Keyframe(currentTime, oldPath.y));
        curveZ.AddKey(new Keyframe(currentTime + diff * speed, startPosition.y));


        clip.SetCurve("", typeof(Transform), "localPosition.x", curveX);
        clip.SetCurve("", typeof(Transform), "localPosition.y", curveY);
        clip.SetCurve("", typeof(Transform), "localPosition.z", curveZ);

        this.animation.AddClip(clip, "clip");
        this.animation.wrapMode = WrapMode.Loop;
        this.animation.Play("clip");
         
    }

    public void UpdateTexture()
    {
        UpdateDirection();

        if (_saveDirection == _direction)
            return;

        

        switch (_direction)
        {
            case Const.ACdirection.LEFT:

                if (_saveDirection == Const.ACdirection.DOWN)
                {
                    this.transform.Rotate(Vector3.up, 90);
                }
                else if (_saveDirection == Const.ACdirection.UP)
                {
                    this.transform.Rotate(Vector3.up, -90);
                }
                else if (_saveDirection == Const.ACdirection.RIGHT)
                {
                    this.transform.Rotate(Vector3.up, 180);
                }

                break;
            case Const.ACdirection.RIGHT:

                if (_saveDirection == Const.ACdirection.DOWN)
                {
                    this.transform.Rotate(Vector3.up, -90);
                }
                else if (_saveDirection == Const.ACdirection.UP)
                {
                    this.transform.Rotate(Vector3.up, 90);
                }
                else if (_saveDirection == Const.ACdirection.LEFT)
                {
                    this.transform.Rotate(Vector3.up, 180);
                }

                break;
            case Const.ACdirection.DOWN:

                if (_saveDirection == Const.ACdirection.RIGHT)
                {
                    this.transform.Rotate(Vector3.up, 90);
                }
                else if (_saveDirection == Const.ACdirection.LEFT)
                {
                    this.transform.Rotate(Vector3.up, -90);
                }
                else if (_saveDirection == Const.ACdirection.UP)
                {
                    this.transform.Rotate(Vector3.up, 180);
                }

                break;
            case Const.ACdirection.UP:

                if (_saveDirection == Const.ACdirection.LEFT)
                {
                    this.transform.Rotate(Vector3.up, 90);
                }
                else if (_saveDirection == Const.ACdirection.RIGHT)
                {
                    this.transform.Rotate(Vector3.up, -90);
                }
                else if (_saveDirection == Const.ACdirection.DOWN)
                {
                    this.transform.Rotate(Vector3.up, 180);
                }
                break;
            case Const.ACdirection.STOP:
                this.rigidbody.velocity = new Vector3(0, 0, 0);
                break;
        }

        _saveDirection = _direction;

        _currentState = Const.ENNEMY_STATE.WALK;
        /*
        Texture tex = texturesAnims[((int)_currentState)];
        this.GetComponent<Anim>().speed = 0.1f;
        this.GetComponent<Anim>().resetTimer(tex, 4);
         * */
        //this.transform.rotation.SetEulerAngles(0, 90, 0);
        
    }

    public void UpdateDirection()
    {
        float diffX = (_savePos.x - this.transform.position.x);
        float diffY = (_savePos.z - this.transform.position.z);

        if (diffX != 0 || diffY != 0)
        {
            _savePos = this.transform.position;
        }
        Vector2 direction = new Vector2(diffX, diffY).normalized;
        if (direction == new Vector2(-1.0f, 0.0f)) // Left
        {
            //Debug.Log("GORIGHT");
            _direction = Const.ACdirection.RIGHT;
        }
        else if (direction == new Vector2(1.0f, 0.0f)) // Right
        {
            //Debug.Log("GOLEFT");
            _direction = Const.ACdirection.LEFT;
        }
        else if (direction == new Vector2(0.0f, 1.0f)) // UP
        {
            //Debug.Log("GODOWN");
            _direction = Const.ACdirection.DOWN;
        }
        else if (direction == new Vector2(0.0f, -1.0f)) // Down
        {
            //Debug.Log("GOUP");
            _direction = Const.ACdirection.UP;
        }
    }
}
